# -*- coding: utf-8 -*-
import random

from django.core.management.base import BaseCommand
from django.contrib.auth.models import User
from django.contrib.webdesign.lorem_ipsum import paragraphs, paragraph, WORDS
from django.contrib.sites.models import Site
from django.template.defaultfilters import slugify

from news.models import News
from projects.models import Project, Release, Screenshot
from cab.models import Snippet, Language, Bookmark

SAMPLE_CODE = '''
"""
A pygame example
"""
import pygame, random

pygame.init()
screen = pygame.display.set_mode([300,100])
screen.fill([255,255,255])

mainloop, x,y, color, fontsize, delta, fps =  True, 25 , 0, (32,32,32), 35, 1, 30
# create clock object
clock = pygame.time.Clock() 

while mainloop:
    tick_time = clock.tick(fps) # milliseconds since last frame
    pygame.display.set_caption("press Esc to quit. FPS: %.2f" % (clock.get_fps()))
    fontsize = random.randint(35, 150)
    myFont = pygame.font.SysFont("None", fontsize)
    color = (random.randint(0,255), random.randint(0,255), random.randint(0,255))
    screen.fill((255,255,255))
    screen.blit(myFont.render("I love the pygame cookbook", 0, (color)), (x,y))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            mainloop = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                mainloop = False
    pygame.display.update()
'''

def words(n):
    return " ".join([random.choice(WORDS) for i in xrange(n)])

class Command(BaseCommand):
    def handle(self, *args, **options):
#        # neccessary:
        
        #Site(domain="pygameweb.no-ip.org", name="pygame web").save()
        #Site(domain="127.0.0.1:8000", name="localhost").save()
        testusers = []
        for i in xrange(30):
            u, c = User.objects.get_or_create(username="user%02.i"%i)
            u.save()
            testusers.append(u)
        
        for obj in Project.objects.all():
            obj.delete()
        tags = ("test %s"%words(15)).split()
        pnames = []
        for i in xrange(20):
            admins = [random.choice(testusers) for i in xrange(random.randint(2, 4))]
            members = [random.choice(testusers) for i in xrange(random.randint(2, 6))]
            while True:
                pname = words(2)
                if pname not in pnames:
                    pnames.append(pname)
                    break
            p = Project(name=pname,
                        markup="rst",
                        description="\n\n".join(paragraphs(4)),
                        summary=paragraph(),
                        homepage_link = "http://example.com",                        
                        creator=admins[0],
                        last_editor=admins[1],
                        tags=",".join([random.choice(tags) for i in xrange(random.randint(3, 10))]),)
            p.save()
            p.admins = admins
            p.members = members
            p.save()
           # s = Screenshot(name="test", project=p,
           #                uploaded_by=User.objects.all()[0],
           #                description="testimage", 
           #                image="screenshots/logo.png")
           # s.save()
            k = 1
            for j in xrange(random.choice((0,0,0,1,1,1,1,2,2,2,3,3,4))):
                r = Release(project=p,
                            version="%i.%i %s"%(k, random.randint(0,9), words(1)),
                            markup="rst",
                            summary="\n\n\n".join(paragraphs(2, False)),
                            changelog=paragraph(),
                            notes_url = "http://example.org",
                            download_url = "http://example.org",
                            creator=members[0],
                            last_editor=members[1])
                r.save()
 

        for obj in News.objects.all():
                    obj.delete()
        news_admins = User.objects.order_by("?")[:4]
        for i in xrange(20):
            n = News(title=words(random.randint(2,5)),
                     slug=slugify("News Test %i"%i),
                        markup="rst",
                    creator=random.choice(news_admins),
                    last_editor=User.objects.all()[0],
                    content="""
Hello,

.. sourcecode:: python

  while True:
    print "Hello World!"


*this site is* `pony powered <http://www.djangopony.com/>`_ 


%s""" % "\n\n".join(paragraphs(3))
                    
                            #    creator=User.objects.all()[0],
                             #   last_editor=User.objects.all()[0]
            )
            n.save()

        py, c = Language.objects.get_or_create(name="Python", language_code="py",
                        file_extension="py", mime_type="application/x-python")
        py.save()
        for i in xrange(10):
            s = Snippet(title=words(random.randint(3, 8)),
                        language=py,
                        author=random.choice(testusers),
                        tags=" ".join([random.choice(tags) for i in xrange(random.randint(2, 8))]),
                        description="".join(paragraphs(2)),
                        code=SAMPLE_CODE,
                        markup="rst"
                        )
            s.save()
            for y in xrange(3):
                b = Bookmark(user=random.choice(testusers), snippet=s)
                b.save()
                
        
             
                        